const db = require(global.__base+'/config/db') //引入上面所讲的数据库基础配置
var userMapper = require('../mapper/userMapper')

async function insertUser() {
    let connection = await db.getTransactionConnection() // 得到链接
    var user = {
        name: '234'
    }
    var role = {
        name: '234'
    }
    var result;
    result = await userMapper.insertUsers(connection, user)
    console.log(result)
    if (result.success === 0) {
        return result;
    }
    result = await userMapper.insertRole(connection, role, result.data.userId)
    console.log(result)
    if (result.success === 0) {
        return result;
    }
    connection.commit((error) => {
        if (error) {
            console.log('事务提交失败')
        }
    })
    connection.release()
    return result;
}

async function getUserList() {
    let connection = await db.getUnTransactionConnection();
    return await userMapper.getUsers(connection)
}

module.exports = {
    insertUser,
    getUserList
}